<?php
Globals::requireClass('Controller');
Globals::requireModel('User');
Globals::requireTable('Goods');
Globals::requireTable('Ordergoods');
Globals::requireTable('Msg');

class ManagerController extends Controller
{
	protected $GoodsTable;
	protected $OrderGoodsTable;
	protected $MsgTable;
	
	public static $defaultConfig = array(
		'viewEnabled'	=> true,
		'layoutEnabled'	=> true,
		'title'			=> null
	);
	
	public function __construct($config = null)
	{
		parent::__construct($config);
		$this->GoodsTable = new GoodsTable($config);
		$this->OrderGoodsTable = new OrdergoodsTable($config);
		$this->MsgTable = new MsgTable($config);
		dolog(json_encode($_GET));
	}
	
	public function indexAction()
	{
		$this->layout->pageTitle	= "test";
	}
	
	//管理员激活商品列表页面
	public function managerrecordAction(){
	    $list = array();
	    $data = $this->OrderGoodsTable->listAll(array('status'=>2));
	    foreach ($data as $k=>$v){
	        $tmp_goods = $this->GoodsTable->getRow($v['goods_id']);
	        
	        $tmp['title'] = $tmp_goods['title'];
	        $tmp['orderuid'] = $v['uid'];
	        $tmp['orderid'] = $v['orderid'];
	        $tmp['time'] = $v['paytime'];
	        $tmp['issuinguid'] = 1374;
	        $list[] = $tmp;
	    }
	    
	    echo json_encode(array('returncode'=>'0','list'=>$list));
	    exit();
	}
	
	//管理员提交激活商品cdkey
	public function sendrechargeresultAction(){
	    $orderid = $this->getParam('orderid');
	    $orderuid = $this->getIntParam('orderuid');
	    
	    $order = $this->OrderGoodsTable->getRow(array('uid'=>$orderuid,'orderid'=>$orderid,'status'=>2));
	    if($order['id'] < 1){
	        echo json_encode(array('returncode'=>'-1'));
	        exit();
	    }else{
	        $cdkey = $this->getParam('cdkey');
	        
	        $this->OrderGoodsTable->update(array('status'=>3,'cdkey'=>$cdkey,'returntime'=>date('Y-m-d H:i:s')),$order['id']);
	        
	        $exreas = array(
	            'content'=>'如果为激活码请尽快激活使用!',
	            'customfield'=>'VipPersonageCdKeyRecharge',
	            'pushtype'=>19
	        );
	        $re = do_jpush($orderuid,'个人会员充值成功!',$exreas);
	        if($re == 1){
	            echo json_encode(array('returncode'=>'0'));
	        }elseif($re == -1){
	            echo json_encode(array('returncode'=>'-2'));
	        }elseif($re == -2){
	            echo json_encode(array('returncode'=>'-3'));
	        }
	        
	        exit();
	    }
	}
	
	//jpush推送
	public function jpushAction(){
	    $pushuid = $this->getParam('pushuid');
	    $pushid = explode(',',$pushuid);
	    if(count($pushid) < 1){
	        echo json_encode(array('returncode'=>'-3','message'=>'失败'));exit();
	    }
	    $alert = $this->getParam('alert');
	    $content = $this->getParam('content');
	    $customfield = $this->getParam('customfield');
	    $customfield = str_replace('*', '&', $customfield);
	    $pushtype = $this->getParam('pushtype');
	    $type = $this->getIntParam('type');
	    
	    if($type == 1){
	        $msg_data['createdate'] = date('Y-m-d H:i:s');
	        foreach ($pushid as $v){
	            $msg_data['uid'] = $v;
	            $msg_data['msg'] = $content;
	            $msg_data['status'] = 0;
	            $this->MsgTable->add($msg_data);
	        }
	    }
	    
	    //$pushid = count($pushid) < 2 ? $pushid[0] : $pushid;
	    $exreas = array('content'=>$content,'customfield'=>$customfield,'pushtype'=>$pushtype);
	    $re = do_jpush($pushid,$alert,$exreas);
	    if($re == 1){
	        echo json_encode(array('returncode'=>'0','message'=>'成功'));
	    }elseif($re == -1){
	        echo json_encode(array('returncode'=>'-1','message'=>'失败'));
	    }elseif($re == -2){
	        echo json_encode(array('returncode'=>'-2','message'=>'失败'));
	    }
	    
	    exit();
	}
	
	protected function out()
	{
		$this->layout->nav		= 'index';
		parent::out();
	}
}

Config::extend('ManagerController', 'Controller');
